//
//  UserHeaderDetailViewController.m
//  EasyChat
//
//  Created by 萱草~ on 16/6/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]


#import "UserHeaderDetailViewController.h"
#import "UserHeaderDetailTableViewCell.h"
#import "UserProfileTableViewCell.h"

static NSString *const CELL = @"cell";
static NSString *const profile = @"Profile";
@interface UserHeaderDetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property(nonatomic,retain)UITableView *myTableView;
@property(retain, nonatomic) NSIndexPath *indexPath;
@end

@implementation UserHeaderDetailViewController
-(void)dealloc{
    [_myTableView release];
    [_indexPath release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"My Profile";
    [self createTableView];
}
-(void)createTableView{
    self.myTableView  = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.myTableView registerClass:[UserHeaderDetailTableViewCell class] forCellReuseIdentifier:CELL];
    [self.myTableView registerClass:[UserProfileTableViewCell class] forCellReuseIdentifier:profile];
    [self.view addSubview:self.myTableView];
//    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_myTableView release];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 95;
    } else {
        return 50;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if (indexPath.row == 0) {
            UserProfileTableViewCell *Profile = [tableView dequeueReusableCellWithIdentifier:profile forIndexPath:indexPath];
//            [Profile.imageBtn addTarget:self action:@selector(imageButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
            Profile.myImageView.image = [UIImage imageNamed:@""];
            Profile.ProfilePhoto.text = @"ProfilePhoto";
//            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            self.indexPath = indexPath;
            return Profile;
        }else{
            UserHeaderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL forIndexPath:indexPath];
            if (indexPath.row == 1) {
                
                cell.Name.text = @"NickName";
                cell.NameLabel.text = @"";
                cell.NameLabel.textAlignment = NSTextAlignmentRight;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }else if (indexPath.row == 2){
                cell.Name.text = @"ID";
                cell.NameLabel.text = @"账号";
                cell.NameLabel.textAlignment = NSTextAlignmentRight;
            }else{
                cell.Name.text = @"二维码";
                cell.NameLabel.text = @"";
//                cell.NameLabel.textAlignment = NSTextAlignmentRight;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
       return cell;
        }
    

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0){
        
        if(IOS8){
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"获取图片" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    //相机
                    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
                    imagePickerController.delegate = self;
                    imagePickerController.allowsEditing = YES;
                    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                    [self presentViewController:imagePickerController animated:YES completion:^{
                        
                    }];
                    [imagePickerController release];
                }];
                [alertController addAction:defaultAction];
                
            }
            UIAlertAction *defaultActionClick = [UIAlertAction actionWithTitle:@"从相册选择" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                //相册
                UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
                imagePickerController.delegate = self;
                imagePickerController.allowsEditing = YES;
                imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:imagePickerController animated:YES completion:^{
                    
                }];
                [imagePickerController release];
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alertController addAction:cancelAction];
            [alertController addAction:defaultActionClick];
            
            //弹出视图 使用UIViewController的方法
            [self presentViewController:alertController animated:YES completion:nil];
        }
        else {
            UIActionSheet *sheet;
            
            //判断是否支持相机.ps:模拟器没有相机
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                sheet = [[UIActionSheet alloc] initWithTitle:@"获取图片" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
            } else {
                sheet = [[UIActionSheet alloc] initWithTitle:@"获取图片" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
            }
            [sheet showInView:self.view];
        }

        
    }else if (indexPath.row == 1){
        UserHeaderDetailTableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
        //准备初始化配置参数
        NSString *title = @"Edit NickName";
        NSString *OKButtonTitle = @"OK";
        NSString *CancelButtonTitle = @"Cancel";
        
        //初始化
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:(UIAlertControllerStyleAlert)];
        
        //创建文本框
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
           textField.placeholder = @"nickname";
            textField.secureTextEntry = NO;
        }];
        
        //创建操作
        UIAlertAction *CancelAction = [UIAlertAction actionWithTitle:CancelButtonTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
           
        }];
        //添加操作(顺序就是程现的上下顺序)
        [alertController addAction:CancelAction];
        
        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:OKButtonTitle style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            //读取文本框的值显示出来
            UITextField *NickName = alertController.textFields.firstObject;
            cell.NameLabel.text = NickName.text;
        }];
        //添加操作(顺序就是程现的上下顺序)
        [alertController addAction:OKAction];
        

        
        //呈现警告视图
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}
#pragma mark - 调用UIActionSheet iOS7使用
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSUInteger sourceType = 0;
    //判断是否支持相机.ps:模拟器没有相机
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        switch (buttonIndex) {
            case 1://相机
                sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
                
            case 2://相机
                sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
        }
    } else {
        if (buttonIndex == 1) {
            sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        }
    }
    
    //跳转到相机或相册页面
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    // 修改了这块  
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentViewController:imagePickerController animated:YES completion:nil];
}

#pragma mark - 保存图片到沙盒
-(void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 1);//1为不缩放保存,取值(0.0-1.0)
    //获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    NSLog(@"%@",fullPath);
    //将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

#pragma mark - iOS7 iOS8都要调用方法,选择完成后调用该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UserProfileTableViewCell *Profile = [self.myTableView cellForRowAtIndexPath:self.indexPath]; //[[UserProfileTableViewCell alloc] init];
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    //保存图片至本地,上传图片到服务器需要使用
    [self saveImage:image withName:@"avatar.png"];
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"avatar.png"];
    
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    
    //设置图片显示
//    [Profile.imageBtn setBackgroundImage:savedImage forState:(UIControlStateNormal)];
    [Profile.myImageView setImage:image];
    NSLog(@"%@", Profile.myImageView.image);

    [savedImage release];
}

#pragma mark - iOS7 iOS8都要调用方法,按取消按钮用该方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
